//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    // 首页导航数据
    navList: [],
    currentIndexNav: 0, // 被点击菜单索引
    swiperList: [], // 轮播图
    // 视频列表
    videoList0: [],
    videoList1: [],
    videoList2: [],
    //下划线css属性
    lineLeft: 0,
    //下划线css属性
    lineWidth: 0
  },

  // 当前导航选项添加下划线
  activeBottomLine(index) {
    let self = this
    wx.createSelectorQuery().select(`#nav${index}`).boundingClientRect((res) => {
      self.setData({
        lineWidth: parseInt(res.width),
        lineLeft: parseInt(res.left)
      })
    })
  },

  // 获取导航数据
  gitNavList() {
    let self = this
    wx.request({
      url: 'https://www.easy-mock.com/mock/5d7865a99fb13c0783c2420c/bilibili/navList',
      success(res) {
        // console.log(res)
        if (res.data.code === 0) {
          self.setData({
            navList: res.data.data.navList
          })
        }
      }
    })
  },

  // 点击首页导航按钮
  activeNav(e) {
    // 给菜单设置下划线
    this.activeBottomLine(e.target.dataset.index)
    this.setData({
      currentIndexNav: e.target.dataset.index
    })
  },

  // 获取轮播图
  getSwiperList() {
    let self = this
    wx.request({
      url: 'https://www.easy-mock.com/mock/5d7865a99fb13c0783c2420c/bilibili/swiperList',
      success(res) {
        // console.log(res)
        if (res.data.code === 0) {
          self.setData({
            swiperList: res.data.data.swiperList
          })
        }
      }
    })
  },

  // 获取视频
  getVideoList() {
    let self = this
    wx.request({
      url: 'https://www.easy-mock.com/mock/5d7865a99fb13c0783c2420c/bilibili/videosList',
      success(res) {
        // console.log(res)
        if (res.data.code === 0) {
          self.setData({
            videoList0: res.data.data.videosList.slice(0,9),
            videoList1: res.data.data.videosList.slice(9,19),
            videoList2: res.data.data.videosList.slice(-10)
          })
        }
      }
    })
  },
  // 视频滑动
  swiperTab(e) {
    // 给菜单设置下滑线
    // console.log(e.detail.current)
    const order = ['nav0', 'nav1', 'nav2', 'nav3', 'nav4', 'nav5', 'nav6', 'nav7', 'nav8', 'nav9', 'nav10', 'nav11', 'nav12', 'nav13', 'nav14', 'nav15', 'nav16', 'nav17', 'nav18',]
    const current = e.detail.current
    if (current >= 0 && current <= 4) {
      this.setData({
        toView: order[0]
      })
    } else if (current >= 5 && current <= 9) {
      this.setData({
        toView: order[5]
      })
    } else if (current >= 10 && current <= 14) {
      this.setData({
        toView: order[10]
      })
    } else if (current >= 15 && current <= 18) {
      this.setData({
        toView: order[15]
      })
    }
    this.activeBottomLine(e.detail.current)

    this.setData({
      currentIndexNav: e.detail.current
    })
  },
  
  onLoad: function () {
    // 获取导航数据
    this.gitNavList()
    // 获取轮播图数据
    this.getSwiperList()
    // 获取视频数据
    this.getVideoList()
  },
  getUserInfo: function(e) {
    
  }
})
